<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="景点" prop="areaId">
        <el-select v-model="queryParams.areaId" placeholder="请选择景点" clearable size="small">
          <el-option
            v-for="dict in listTouristAreaOptions"
            :key="dict.addrCode"
            :label="dict.addrName"
            :value="dict.addrCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="profile">
        <el-select v-model="queryParams.profile" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="dict in touristInsightProfileOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="纬度" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择纬度" clearable size="small">
          <el-option
            v-for="dict in touristTimeTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item label="日期" prop="touristDay">
        <el-date-picker
          v-model="dateRange"
          type="datetimerange" value-format="yyyy-MM-dd HH:mm"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['app:touristInsight:export']"
        >导出</el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="touristInsightList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="景点" align="center" prop="areaName" />
      <el-table-column label="类型" align="center" prop="profile" :formatter="touristInsightProfileFormat"/>
      <el-table-column label="纬度" align="center" prop="type" :formatter="touristTimeTypeFormat"/>
      <el-table-column label="日期" align="center" prop="touristDay" width="180" />
      <el-table-column label="结果1" align="center" prop="result1" />
      <el-table-column label="结果值1" align="center" prop="resultName1" />
      <el-table-column label="人数" align="center" prop="quantity" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改实时客流洞察对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="区域" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入区域" />
        </el-form-item>
        <el-form-item label="日期" prop="touristDay">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.touristDay"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结果1" prop="result1">
          <el-input v-model="form.result1" placeholder="请输入结果1" />
        </el-form-item>
        <el-form-item label="结果值1" prop="resultName1">
          <el-input v-model="form.resultName1" placeholder="请输入结果值1" />
        </el-form-item>
        <el-form-item label="总人数" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入总人数" />
        </el-form-item>
        <el-form-item label="类型" prop="profile">
          <el-input v-model="form.profile" placeholder="请输入类型" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTouristInsight, getTouristInsight, delTouristInsight, addTouristInsight, updateTouristInsight } from "@/api/app/touristInsight";
import { listTouristArea } from "@/api/app/touristAddrArea";

export default {
  name: "TouristInsight",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      dateRange: [],
      // 实时客流洞察表格数据
      touristInsightList: [],
      touristInsightProfileOptions: [],
      touristTimeTypeOptions: [],
      listTouristAreaOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaId: null,
        touristDay: null,
        result1: null,
        resultName1: null,
        quantity: null,
        profile: null,
        orderByColumn: "tourist_day",
        isAsc: "desc",
        type: 1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    // this.getList();
    this.getDicts("TouristInsightProfile").then(response => {
      this.touristInsightProfileOptions = response.data;
    });
    this.getDicts("TouristTimeType").then(response => {
      this.touristTimeTypeOptions = response.data;
    });
    listTouristArea({}).then().then(response => {
      this.listTouristAreaOptions = response;
    });
  },
  methods: {
    /** 查询实时客流洞察列表 */
    getList() {
      this.loading = true;
      listTouristInsight(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.touristInsightList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    touristInsightProfileFormat(row, column) {
      return this.selectDictLabel(this.touristInsightProfileOptions, row.profile);
    },
    touristTimeTypeFormat(row, column) {
      return this.selectDictLabel(this.touristTimeTypeOptions, row.type);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        areaId: null,
        touristDay: null,
        result1: null,
        resultName1: null,
        quantity: null,
        profile: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实时客流洞察";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTouristInsight(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改实时客流洞察";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTouristInsight(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addTouristInsight(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除实时客流洞察编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTouristInsight(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('app/touristInsight/export', {
        ...this.queryParams
      }, `app_touristInsight.xlsx`)
    }
  }
};
</script>
